let http = require('../../../utils/api.js');
import {
    toData,
    myHttps
} from '../../../utils/util.js';
Page({
    data: {
        hiddenLoading: false,
        items: [], //所有收藏
        startX: 0, //开始坐标
        startY: 0,
        userId: '', //用户id
        size: 10, //页容量
        current: 1, //页码
        total: '', //收藏的数量
    },

    onLoad: function(e) {
        this.mycollect(e.userId, this.data.size, this.data.current)
        this.setData({
            userId: e.userId
        })
        var that = this;
        //common是自己写的公共JS方法，可忽略
        //common.sys_main(app, that, e);
        this.setData({
            items: this.data.items
        });

    },
    // 点击收藏每一条 跳转到文章页面
    goDetails(e) {
        let id = e.currentTarget.dataset.id
        // console.log(id)
        wx.navigateTo({
            url: '../../details/details?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 点击解读 去解读详情
    gounscrambleDetails(e) {
        let id = e.currentTarget.dataset.id
        // console.log(id)
        wx.navigateTo({
            url: '../../unscrambleDetails/unscrambleDetails?id=' + id,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    //手指触摸动作开始 记录起点X坐标
    touchstart: function(e) {
        // console.log(this.data.items)

        //开始触摸时 重置所有删除

        this.data.items.forEach(function(v, i) {

            if (v.isTouchMove) //只操作为true的

                v.isTouchMove = false;

        })

        this.setData({

            startX: e.changedTouches[0].clientX,

            startY: e.changedTouches[0].clientY,

            items: this.data.items

        })

    },
    //滑动事件处理
    touchmove: function(e) {

        var that = this,

            index = e.currentTarget.dataset.index, //当前索引

            startX = that.data.startX, //开始X坐标

            startY = that.data.startY, //开始Y坐标

            touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标

            touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标

            //获取滑动角度

            angle = that.angle({
                X: startX,
                Y: startY
            }, {
                X: touchMoveX,
                Y: touchMoveY
            });

        that.data.items.forEach(function(v, i) {

            v.isTouchMove = false

            //滑动超过30度角 return

            if (Math.abs(angle) > 30) return;

            if (i == index) {

                if (touchMoveX > startX) //右滑

                    v.isTouchMove = false

                else //左滑

                    v.isTouchMove = true

            }

        })

        //更新数据

        that.setData({

            items: that.data.items

        })

    },
    /**
    * 计算滑动角度
    * @param {Object} start 起点坐标
    * @param {Object} end 终点坐标
    */
    angle: function(start, end) {

        var _X = end.X - start.X,

            _Y = end.Y - start.Y

        //返回角度 /Math.atan()返回数字的反正切值

        return 360 * Math.atan(_Y / _X) / (2 * Math.PI);

    },
    //取消收藏文章
    delEssay (id,index) {
        let url = http.api.cancelCollect + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.data.items.splice(index, 1)
                this.setData({
                    items: this.data.items,
                    hiddenLoading: true,
                    total: this.data.total-1
                })
                wx.setNavigationBarTitle({
                    title: '我的收藏（' + this.data.total + '）' //页面标题为路由参数
                })
            }
        }).catch(err => {})
    },
    // 解读取消收藏
    cancelcollects(id,index) {
        let url = http.api.reviewcollect + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.data.items.splice(index, 1)
                this.setData({
                    items: this.data.items,
                    hiddenLoading: true,
                    total: this.data.total - 1
                })
                wx.setNavigationBarTitle({
                    title: '我的收藏（' + this.data.total + '）' //页面标题为路由参数
                })
            }
        }).catch(err => { })
    },
    //取消收藏接口
    del: function(e) {
        let types = e.currentTarget.dataset.types
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        // console.log(types)
        if (types == 'Essay') {//文章
            this.delEssay(id,index)
        } else if (types == 'Review') {//解读
            this.cancelcollects(id,index)
        }
        
    },
    // 获取收藏列表接口
    mycollect(id, size, page) {
        let url = http.api.mycollect + id + '/favs?size=' + size + '&page=' + page;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    if (res.data.content[i].content.pubdate){
                        res.data.content[i].content.pubdate = toData(res.data.content[i].content.pubdate)
                    } else if (res.data.content[i].content.update_time) {
                        res.data.content[i].content.update_time = toData(res.data.content[i].content.update_time)
                    }
                    this.data.items.push(res.data.content[i])
                }
                this.setData({
                    items: this.data.items,
                    hiddenLoading: true,
                    total:res.data._meta.total
                })
                wx.setNavigationBarTitle({
                    title: '我的收藏（' + this.data.total + '）' //页面标题为路由参数
                })
            }
        }).catch(err => {})
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function() {
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            items: [],
            size: 10,
            current: 1
        })
        this.mycollect(this.data.userId, this.data.size, this.data.current);
    },
    // 上拉加载五条数据
    onReachBottom: function() {
        this.setData({
            size: 10,
            current: this.data.current + 1,
        })
        this.mycollect(this.data.userId, this.data.size, this.data.current);
    },
})